Struct Vs Class When To Use Which ยท TL;DR

1 min read
Mid-level7 min read
Rapid overview

TL;DR

Featurestructclass
Type categoryValue typeReference type
Memory allocationStored inline wherever its container lives โ€” a local's stack slot (or a register), a field inside a heap object, an array element. Boxed copies go to the heapInstance on the heap; the variable holds a reference
Default behaviorCopied by value (creates a full copy)Copied by reference (points to same object)
NullabilityCannot be null (unless Nullable<T>)Can be null
InheritanceCannot inherit from another struct or class; only from ValueTypeSupports inheritance and polymorphism
InterfacesCan implement interfacesCan implement interfaces and base classes
Default constructorAlways has an implicit public parameterless ctor that zeroes every field. C# 10+ lets you declare one, but it must be public, and default(T) / new T[n] skip itCan freely define constructors
EqualityValueType.Equals compares fields (may use reflection โ€” slow); no == unless you define it (record struct generates both)Reference equality unless overridden (record generates value equality)
Recordsrecord struct / readonly record struct (C# 10)record / record class (C# 9)
Finalizer / DestructorNot supportedSupported
GC behaviorNot a separate GC object: reclaimed with its container (stack frame pops, or the owning heap object is collected)Each instance tracked by the Garbage Collector
Boxing / UnboxingConverting to/from object/interface causes allocationNo boxing/unboxing issues
Thread safetySafer for small immutable dataReference types require synchronization if shared

See also